/**
 * Created by daigou on 2015/5/29.
 */
var getReview = require('./getTaobaoReview');
var getProductList = require('./getProductList');
var config = require('./config.json');
var saveReview = require('./saveUserReview');

console.time('get user review start.');
getProductList(function (data) {
    getEachReview();

    function getEachReview() {
        var item = data.shift();
        getReview(item.taobaoID, 1, function (data) {
            data.rateList && saveReview(item, data.rateList);
        });
        if (data.length > 0) {
            setTimeout(getEachReview, config.interval);
        } else {
            console.timeEnd('get user review start.');
            console.log('get review for all product complete!');
        }
    }
});

/*var idPattern = /id=(\d*)/;

 ['http://detail.tmall.com/item.htm?spm=a230r.1.14.1.UAJ7ax&id=43932385009&cm_id=140105335569ed55e27b&_u=31veb3a88193&abbucket=3',
 'http://detail.tmall.com/item.htm?spm=a1z10.5-b.w4011-8749712220.85.HBbNT7&id=43466582056&rn=a1a823d00c293b7dbf380b80ca9049a0&abbucket=3&sku_properties=5919063:6536025',
 'http://detail.tmall.com/item.htm?id=39312587049',
 'http://detail.tmall.com/item.htm?spm=a230r.1.14.171.ujQrqp&id=45115208685&ns=1&abbucket=3&skuId=84402169464',
 'http://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.8.sx8y5d&id=40491073692&skuId=76512680711&areaId=310100&cat_id=2&rn=96fe50b6c1f5fa7ca70ec39bdff3ad98&user_id=1714128138&is_b=1',
 'http://item.taobao.com/item.html?id=44480146995',
 'http://item.taobao.com/item.htm?spm=a230r.1.14.9.MCgZFx&id=43117939594&ns=1&abbucket=1',
 'http://item.taobao.com/item.htm?spm=a230r.1.14.24.eSWb0a&id=43512840031&ns=1&abbucket=1',
 'http://item.taobao.com/item.htm?spm=a230r.1.0.0.1r9ToB&id=16005330306&ns=1'].forEach(function (item, ind) {
 var match = idPattern.exec(item);
 if (match) {
 var id = parseInt(match.pop());
 setTimeout(function () {
 getReview(id, 1, function (data) {
 data.rateList && saveReview({url: item}, data.rateList);
 });
 }, ind * 3000);
 }
 });*/
